
<template>
  <div class="page-container">
    <el-form ref="form" :model="form" :label-width="$globalCss.formLabelWidth_default" :rules="formRules"
      v-loading="loading" label-position="right" class="common-form-panel-div">

      <ex_header_spiler :title="$t('label_base_info')"></ex_header_spiler>

      <el-row>
        <el-col>
          <el-form-item :label="$t('label_network_code')" prop="placeCode">
            <ex_super_input v-model="form.placeCode" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item :label="$t('label_network_name')" prop="placeName">
            <ex_super_input v-model="form.placeName" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item :label="$t('label_network_belong')" prop="parentPlaceId">
            <ex_super_input v-model="form.parentPlaceId" type="select" class="w_fill"
              url="/tms-api/base/TmsServiceOutletRpc/selectLikeServiceOutletLabel.json"
              :option="(e) => ({ label: e.placeName, value: e.id })" :urlParams="{ id }"
              @changeItem="parentPlaceChange" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item :label="$t('label_network_center')" prop="serviceOutletCenterId">
            <ex_super_input v-model="form.serviceOutletCenterId" type="select" class="w_fill" :urlParams="{ id }"
              url="/tms-api/base/TmsServiceOutletRpc/selectLikeServiceCenter.json"
              :option="(e) => ({ label: e.placeName, value: e.id })" @changeItem="networkCenterChange" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item :label="$t('label_belong_headquarters')">
            <ex_super_input v-model="form.serviceOutletHeadOfficeName" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item :label="$t('label_belong_region')">
            <ex_super_input v-model="form.serviceOutletRegionName" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item :label="$t('label_belong_partition')">
            <ex_super_input v-model="form.serviceOutletPartitionName" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item :label="$t('label_network_place')">
            <ex_super_input v-model="form.placeLocation" class="w_fill" type="select" enum="TmsBasePlacePositionEnum" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item :label="$t('label_network_mode')">
            <ex_super_input v-model="form.serviceOutletMode" class="w_fill" type="select"
              enum="TmsBaseServiceOutletModeEnum" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item :label="$t('label_network_type')">
            <ex_super_input v-model="form.serviceOutletType" class="w_fill" type="select"
              enum="TmsBaseServiceOutletTypeEnum" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item :label="$t('label_srm_supplier_contact_name')">
            <ex_super_input v-model="form.contacts" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item :label="$t('label_srm_supplier_contact_phone')">
            <ex_super_input v-model="form.contactNumber" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item :label="$t('label_financial_center')">
            <ex_super_input v-model="form.financialCenter" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item :label="$t('label_distribution_center')">
            <ex_super_input v-model="form.distributionCenter" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item :label="$t('label_allow_arrive_pay')">
            <ex_super_input v-model="form.allowArrivePay" class="w_fill" type="select" enum="YesOrNoEnum" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item :label="$t('label_pre_pay_enable')">
            <ex_super_input v-model="form.isPrepayEnabled" class="w_fill" type="select" enum="IsAvailableStatusEnum" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item :label="$t('label_network_area')" prop="countryCode">
            <cascader_address_select v-if="id && form.countryCode || !id || !form.countryCode" class="w_fill"
              :countryCode.sync="form.countryCode" :countryName.sync="form.countryName" :province.sync="form.province"
              :city.sync="form.city" :county.sync="form.county" :zipCode.sync="form.postalCode" :checkStrictly="false" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item :label="$t('label_detail_address')">
            <el-input v-model="form.address" type="textarea" :autosize="{ minRows: 2, maxRows: 4 }"
              :placeholder="$t('tip_please_input')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <!-- <el-col>
            <el-form-item :label="$t('label_lon')" prop="longitude">
              <ex_super_input v-model="form.longitude" />
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item :label="$t('label_lat')" prop="latitude">
              <ex_super_input v-model="form.latitude" />
            </el-form-item>
          </el-col> -->
        <el-col>
          <el-form-item :label="$t('label_service_range_remark')">
            <el-input v-model="form.serviceRangeRemark" type="textarea" :autosize="{ minRows: 2, maxRows: 4 }"
              :placeholder="$t('tip_please_input')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <span>
          <el-button type="primary" @click="submit" :loading="submiting" :disabled="submiting">{{ $t('label_save')
          }}</el-button>
          <el-button @click="pageBack">{{ $t('label_fms_account_config_cancel') }}</el-button>
        </span>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import router_params from '@/mixin/router_params';
import cascader_address_select from '@/component/tms/cascader_address_select'
import country_select from '@/component/tms/country_select'

let defaultObj = {
  placeCode: '',
  placeName: '',
  parentPlaceId: '',// 父级网点id
  parentPlaceCode: '',// 父级网点id
  parentPlaceName: '',// 父级网点id
  placeType: 'SERVICE_OUTLET',
  status: 'ENABLE',
  serviceOutletCenterId: '',
  serviceOutletCenterCode: '',
  serviceOutletCenterName: '',
  serviceOutletHeadOfficeName: '', // 总部名称
  serviceOutletRegionName: '', // 大区名称
  serviceOutletPartitionName: '', // 分区名称
  serviceOutletMode: '',
  serviceOutletType: 'SERVICE_OUTLET',
  placeLocation: 'DOMESTIC',// 默认国内
  contacts: '',
  contactNumber: '',
  address: '',
  serviceRangeRemark: '',
  financialCenter: '',
  distributionCenter: '',
  allowArrivePay: 'N',
  isPrepayEnabled: 'DISABLED',
  countryCode: '',
  countryName: '',
  county: ''
}
export default {
  name: 'place_operate',
  mixins: [router_params],

  components: { cascader_address_select, country_select },
  data() {
    return {
      id: null,
      edit: false,
      loading: false,
      submiting: false,
      form: _.cloneDeep(defaultObj),
      formRules: {
        placeCode: { required: true, message: this.$t('tip_can_no_empty'), trigger: 'blur' },
        placeName: { required: true, message: this.$t('tip_can_no_empty'), trigger: 'blur' },
        countryCode: { required: true, message: this.$t('tip_can_no_empty'), trigger: 'blur' },
        longitude: [
          { required: true, message: this.$t('tip_can_no_empty'), trigger: 'blur' },
          { pattern: /^-?((0|[1-9]\d?|1[0-7]\d)(\.\d{1,6})?|180(\.0{1,6})?)$/, message: this.$t('label_lonreg_msg') }
        ],
        latitude: [
          { required: true, message: this.$t('tip_can_no_empty'), trigger: 'blur' },
          { pattern: /^-?((0|[1-8]\d?)(\.\d{1,6})?|90(\.0{1,6})?)$/, message: this.$t('label_latreg_msg'), trigger: 'blur' }
        ],
        // parentPlaceId: { required: true, message: this.$t('tip_can_no_empty'), trigger: 'blur' },
        // serviceOutletCenterId: { required: true, message: this.$t('tip_can_no_empty'), trigger: 'blur' },
      }
    };
  },


  methods: {
    init_data_default(params) {
      if (params.edit) this.edit = params.edit
      if (this.edit) {
        if (params.id) {
          this.id = params.id
          this.loading = true
          this.$ajax({
            url: "/tms-api/base/TmsServiceOutletRpc/detail.json",
            method: 'post',
            data: { id: params.id },
            success: (res) => {
              this.loading = false
              this.form = Object.assign({}, _.cloneDeep(defaultObj), res.data)

              this.$emit("tagModifyLabel", { _key: 'label_network_edit', code: this.form.placeName || "" })
            }
          })
        }

        this.$emit("tagModifyLabel", { _key: 'title_edit_place', code: this.$t("tip_loading") })

      } else {

        this.$emit('tagModifyLabel', "label_network_create")
      }
    },

    pageBack() {
      this.$tag.tagClose(null, this.intoPath)
    },

    submit() {
      if (this.$clickFast()) return
      this.$refs['form'].validate((valid, obj) => {

        if (valid) {
          this.submiting = true
          this.$ajax({
            url: "/tms-api/base/TmsServiceOutletRpc/saveOrUpdate.json",
            method: 'post', type: 'json',
            loading: true,
            data: this.form,
            success: (res) => {
              this.$message.success(this.$t('tip_submit_success'))
              this.return_emit(null, "reload")
              this.pageBack()
            },
            complete: () => {
              this.submiting = false
            }
          })

        } else {
          this.$message.error(this.$t('form_validate_error'))
          return false;
        }
      });

    },

    parentPlaceChange(e) {
      this.form.parentPlaceCode = e.placeCode
      this.form.parentPlaceName = e.placeName
    },
    networkCenterChange(e) {
      this.form.serviceOutletCenterCode = e.placeCode
      this.form.serviceOutletCenterName = e.placeName
    }
  }
};

</script>
<style></style>
